home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Use Property Procedures

Use property procedures instead of public variables to expose values outside a form, class, or module. Property procedures give you more control over the value. If you later need to change the way the value is stored, you can do so without modifying the outside program. Property procedures also allow you to create read-only values.

For example, suppose a dispatch program uses a Map class to store map information. The Distance property get procedure shown in the following code returns the shortest distance between two points. Because there is no corresponding property let procedure, the application can read this value but cannot change it.

‘ The number of locations.
Private mNumLocations As Integer

‘ Private distance array. Dimensioned
‘     (1 To mNumLocations, 1 To mNumLocations)
Private mDistance() As Single

‘ Other declarations.
     :
‘ Return the distance between two locations.
Property Get Distance(ByVal from_point As Integer, _
    ByVal to_point As Integer)

    Distance = mDistance(from_point, to_point)
End Property

Now suppose the program grows until the map is too large to store all the distance information in an array. The map of a moderately large city might contain 10,000 locations. Storing all the distances between pairs of locations in an array would require 10,000 * 10,000 = 100 million entries taking up 400MB of memory.

In that case, you might rewrite the Distance property get procedure so it calculates the distance between two points whenever it is needed instead of storing the distances in an array. As long as the procedure’s parameters and return value remain unchanged, the rest of the program will continue to work without changes.

Property procedures also provide an important debugging opportunity. Suppose you know the value ProgramCount is being set to an invalid value somewhere in a complicated piece of code. If ProgramCount is a public variable, you must search out every place it is set and look for the invalid change. If ProgramCount is a property procedure, you can simply set a break point inside the procedure. Then when you run the program in the debugger, you can see exactly where the value is changed. You can use the debugger’s call stack command to see which routine is modifying the value and where.

Many programmers overlook the fact that you can create property procedures in a .BAS module. Property procedures in .BAS modules give you all the advantages of property procedures in classes and forms, including better encapsulation and the equivalent of read-only public variables.

Use Implements

Visual Basic 5 introduced the Implements keyword. You can use Implements to make the code more manageable and to improve performance in Visual Basic 5 and 6.

Suppose your program needs to use more than one class that provides the same features. For example, the Car and Truck classes might both provide Location, Speed, Mileage, and FuelCapacity property procedures. In addition, they would also provide features that are specific to their different kinds of objects. In cases such as this, you can define an interface class to define the features shared by the Car and Truck classes.

Create a new Vehicle class. In this class, define the features that Car and Truck should share. For this example, create public variables named Location, Speed, Mileage, and FuelCapacity.

Next, create a new Car class. After the class module’s Option Explicit statement, add the statement Implements Vehicle. This statement makes Visual Basic create a new Vehicle symbol in the Car class. It also makes Visual Basic build property procedure stubs for the public variables defined in the Vehicle class. If you open the class in the Visual Basic editor, you will find the name Vehicle in the object box in the upper left of the edit window. The procedures box lists the public symbols defined by the Vehicle class. Figure 10.1 shows the Visual Basic editor displaying an empty Vehicle object’s Mileage property get procedure.

You still need to create the procedures defined by the Vehicle class yourself, but using Implements has several advantages. First, it standardizes the interface defined by the Vehicle class. You do not need to worry about the Car providing Speed as an integer and the Truck class providing it as a single. Speed is defined for both classes by the Vehicle class so they must be the same.


Figure 10.1  The Vehicle_Mileage property procedure defined by the Vehicle class.

Implements also allows the program to treat Car and Truck objects polymorphically. This means that they can be used interchangeably under some circumstances.

Suppose the MakeTrip subroutine takes as a parameter either a Car or Truck object and then simulates a trip by that object. The program can pass this routine either a Car or a Truck object. As long as the routine uses only properties and methods that are actually implemented by the object, it does not need to know what kind of object it is. The following code shows how this routine might be defined.

Public Sub MakeTrip(veh As Object)
    ‘ Do whatever is needed for the trip.
         :
End Sub

Unfortunately, the program could also pass this routine any other kind of object including a Form or a control. If the program uses the Implements keyword to make Car and Truck implement the Vehicle interface, you can declare the parameter to be of type Vehicle instead of type Object. That lets Visual Basic verify that the object passed to the routine actually implements the Vehicle interface. It can provide better error checking and it allows Visual Basic to manage the object more efficiently.

Public Sub MakeTrip(veh As Vehicle)
    ‘ Do whatever is needed for the trip.
         :
End Sub

Use the Implements keyword to standardize the features implemented by similar classes. Implements keeps the classes synchronized, allows Visual Basic to perform better error checking, and gives better performance.

Encapsulate with Types

In some ways, user-defined types (UDTs) are similar to classes without routines. A UDT is a simple data structure that groups related pieces of data together in a convenient package. For example, the following code shows part of the definition of an Employee data structure.

Type Employee
    empLastName As String
    empFirstName As String
    empSalary As Single
    empId As Integer
    empSSN As String * 11
    empHomePhone As String * 12
    empOfficePhone As String * 12
        :
End Type

User-defined types encapsulate the data needed to represent a complex object. They allow the program to manipulate data entities easily while ignoring any unnecessary details. For example, a payroll application might use the previously defined Employee type. Different payroll routines could pass an employee’s data around without worrying about the details of what was inside. When a routine needed some data, it would be readily available. For instance, the subroutine that actually generates checks could examine the data structure’s name, address, Social Security number, and other fields as necessary.

Use UDTs to hide complexity from the program while keeping all of the data readily available for those routines that need it.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.